package com.demo.framework.utils;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

/**
 * 
 * @ClassName: PhoneReceiver
 * @Description: 对来去电进行监听
 * @author 谢云腾
 * @date 2013-3-22 上午11:16:07
 * @version 1.0
 */
public class PhoneReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        try {
            if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
                // 拨打
            }
            else {
                // 接听
                TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
                tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
            }
        }
        catch(Exception e) {
            // TODO: handle exception
        }
    }

    PhoneStateListener listener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            // TODO Auto-generated method stub
            // state 当前状态 incomingNumber,貌似没有去电的API
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                System.out.println("挂断");
                // LOG.info("onCallStateChanged CALL_STATE_IDLE 挂断");
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                System.out.println("接听");
                // LOG.info("onCallStateChanged CALL_STATE_OFFHOOK 接听");
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                // 输出来电号码
                break;
            }
        }

    };
}
